package com.dxtx.widget;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;


//像山河画卷一样展开的View
public class PictureScrollView extends FrameLayout {

    private float mHeight, mWidth;

    // 从0到完全展开的时间
    private long completelyOpenTime = 250;

    // 清除画笔
    private Paint mClearPaint, paint;


    public PictureScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public PictureScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PictureScrollView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mClearPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mClearPaint.setColor(Color.BLUE);
        mClearPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        paint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        mClearRect.set(0, mHeight, mWidth, mHeight);
        rect.set(0, 0, mWidth, mHeight);
        invalidate();
    }

    private RectF rect = new RectF(), mClearRect = new RectF();

    @Override
    protected void dispatchDraw(Canvas canvas) {


        int saveCount = canvas.saveLayer(rect, paint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);

        canvas.drawRect(mClearRect, mClearPaint);
        canvas.restoreToCount(saveCount);

    }


    private ValueAnimator animator;
    private AnimatorUpdateListener updateListener = new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            float v = (Float) animator.getAnimatedValue();
            mClearRect.set(0, v, mWidth, mHeight);
            invalidate();
        }
    };

    public void close() {
        if (animator != null) {
            animator.cancel();
        }
        animator = ValueAnimator.ofFloat(mHeight, 0);
        animator.addUpdateListener(updateListener);
        animator.setDuration(completelyOpenTime);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }

    public void open() {
        if (animator != null) {
            animator.cancel();
        }
        animator = ValueAnimator.ofFloat(0, mHeight);
        animator.addUpdateListener(updateListener);
        animator.setDuration(completelyOpenTime);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }

    public void closeAndOpen() {
        if (animator != null) {
            animator.cancel();
        }
        animator = ValueAnimator.ofFloat(mHeight, 0);
        animator.addUpdateListener(updateListener);
        animator.setDuration(completelyOpenTime);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setRepeatCount(1);
        animator.start();
    }

}
